package com.yt.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.user.models.service.IVirtualNumberService;
import com.yt.user.models.bo.VirtualNumberBO;
import com.yt.user.models.dto.VirtualNumberDTO;
import com.yt.user.models.bo.VirtualNumberSaveBO;

import java.util.List;

/**
 * 虚拟号码信息控制器
 *
 * @author yzz
 * @Date 2025-10-22 09:49:44
 */

@RestController
@RequestMapping("/api/virtualNumber")
@Slf4j
@Api(value = "VirtualNumberApiController",tags = "虚拟号码信息Api接口")
public class VirtualNumberApiController {

    @DubboReference
    private IVirtualNumberService virtualNumberService;

    @PostMapping("/add")
    @ApiOperation(value = "添加虚拟号", notes = "添加新的虚拟号码信息")
    public Result<Boolean> add(@RequestBody VirtualNumberSaveBO saveBO) {
        try {
            boolean result = virtualNumberService.add(saveBO);
            if (result) {
                return new Result<>(true);
            } else {
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "添加失败");
            }
        } catch (Exception e) {
            log.error("添加虚拟号异常", e);
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "添加虚拟号异常: " + e.getMessage());
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态", notes = "修改虚拟号码状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "virtualNumber", value = "虚拟号码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "虚拟号状态（0-未启用，1-正常使用，2-已注销）", required = true, paramType = "query", dataType = "Integer")
    })
    public Result<Boolean> updateStatus(@RequestParam("virtualNumber") String virtualNumber, @RequestParam("status") Integer status) {
        try {
            if (virtualNumber == null || virtualNumber.isEmpty()) {
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "虚拟号码不能为空");
            }
            
            if (status == null) {
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "状态不能为空");
            }

            VirtualNumberDTO dto = new VirtualNumberDTO();
            dto.setVirtualNumber(virtualNumber);
            dto.setStatus(status);
            boolean flag = virtualNumberService.updateById(dto);
            if (flag) {
                return new Result<>(true);
            } else {
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "更新状态失败");
            }
        } catch (Exception e) {
            log.error("修改虚拟号状态异常", e);
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "修改虚拟号状态异常: " + e.getMessage());
        }
    }

    @ApiOperation(value = "删除虚拟号码信息", notes = "删除虚拟号码信息")
    @ApiImplicitParam(name = "virtualNumber", value = "虚拟号码", required = true, paramType = "query", dataType = "String")
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestParam("virtualNumber") String virtualNumber) {
        try {
            if (virtualNumber == null || virtualNumber.isEmpty()) {
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "虚拟号码不能为空");
            }

            // 先查询是否存在该虚拟号码
            Wrapper<VirtualNumberDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("virtual_number", virtualNumber);
            VirtualNumberDTO existing = virtualNumberService.selectOne(wrapper);
            
            if (existing == null) {
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "虚拟号码不存在");
            }
            
            // 执行删除操作
            boolean flag = virtualNumberService.deleteById(virtualNumber);
            if (flag) {
                return new Result<>(true);
            } else {
                return new Result<>(CodeEnum.FAIL_100002.getCode(), "删除失败");
            }
        } catch (Exception e) {
            log.error("删除虚拟号异常", e);
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "删除虚拟号异常: " + e.getMessage());
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<VirtualNumberDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        VirtualNumberDTO dto = virtualNumberService.selectById(id);

        return new Result<>(dto);
    }
    
    @PostMapping("/getByPhoneNumber")
    @ApiOperation(value = "根据手机号查找", notes = "根据手机号查找虚拟号码信息")
    @ApiImplicitParam(name = "phoneNumber", value = "手机号", required = true, paramType = "query", dataType = "String")
    public Result<List<VirtualNumberDTO>> getByPhoneNumber(@RequestParam("phoneNumber") String phoneNumber) {
        if (phoneNumber == null || phoneNumber.isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "手机号不能为空");
        }
        
        Wrapper<VirtualNumberDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("owner_phone", phoneNumber);
        List<VirtualNumberDTO> dtoList = virtualNumberService.selectList(wrapper);
        
        return new Result<>(dtoList);
    }

    @ApiOperation(value = "虚拟号码信息列表",notes = "虚拟号码信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<VirtualNumberDTO>> queryPageList(VirtualNumberBO bo){
        Wrapper<VirtualNumberDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<VirtualNumberDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = virtualNumberService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}